package com.yequan.exesvr.service;


import com.yequan.exesvr.listener.ExeResultMsgMqMessageListener;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author yq
 * @date 2020/08/31
 */
@Service
public class ResultMsgConsumerService {

    private DefaultMQPushConsumer consumer = null;


    @PostConstruct
    public void initMQConsumer() {
        consumer = new DefaultMQPushConsumer("msgGroup");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        try {
            consumer.setMessageModel(MessageModel.BROADCASTING);
            consumer.subscribe("exe-resultMsg-topic", "*");
            consumer.registerMessageListener(new ExeResultMsgMqMessageListener());
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }

    @PreDestroy
    public void shutDownConsumer() {
        if (consumer != null) {
            consumer.shutdown();
        }
    }

}
